export const domUtils = {
    getSelectionPosition() {
        const selection = window.getSelection();
        const range = selection.getRangeAt(0);
        return range.getBoundingClientRect();
    },

    // 计算卡片位置
    calculateCardPosition(position, cardWidth, cardHeight) {
        const SPACING = 8;
        const SCREEN_MARGIN = 10;
        
        // 获取工具栏元素
        const toolbar = document.querySelector('#floating-toolbar');
        const toolbarRect = toolbar?.getBoundingClientRect();
        
        const viewportWidth = window.innerWidth;
        const viewportHeight = window.innerHeight;
        
        let left, top;
        
        if (toolbarRect) {
            // 如果找到工具栏，将卡片放在工具栏下方
            left = toolbarRect.left;
            top = toolbarRect.bottom + SPACING;
            
            // 如果下方空间不足，则显示在工具栏上方
            if (top + cardHeight + SCREEN_MARGIN > viewportHeight) {
                top = toolbarRect.top - cardHeight - SPACING;
            }
        } else {
            // 如果找不到工具栏，使用传入的位置参数
            left = Math.min(
                Math.max(SCREEN_MARGIN, position.x),
                viewportWidth - cardWidth - SCREEN_MARGIN
            );
            
            top = position.y + SPACING;
            if (top + cardHeight + SCREEN_MARGIN > viewportHeight) {
                top = position.y - cardHeight - SPACING;
            }
        }
        
        // 确保卡片不会超出屏幕左右边界
        left = Math.min(
            Math.max(SCREEN_MARGIN, left),
            viewportWidth - cardWidth - SCREEN_MARGIN
        );
        
        return {
            left,
            top,
            maxHeight: viewportHeight - top - SCREEN_MARGIN
        };
    },

    createElementWithStyles(tagName, styles) {
        const element = document.createElement(tagName);
        Object.assign(element.style, styles);
        return element;
    },

    getSelectedText() {
        return window.getSelection().toString().trim();
    },

    removeElement(element) {
        // 添加安全检查
        if (element && element.parentNode) {
            element.parentNode.removeChild(element);
        }
    },

    calculateToolbarPosition(selectionRect) {
        const PADDING = 12;        // 屏幕边缘安全距离
        const SPACING = 8;         // 与鼠标/选中文本的间距
        
        // 获取工具栏尺寸
        const toolbar = document.querySelector('.floating-toolbar');
        const toolbarWidth = toolbar?.offsetWidth || 200;
        const toolbarHeight = toolbar?.offsetHeight || 40;

        // 获取鼠标位置（优先使用鼠标位置）
        const mouseX = window.event?.clientX || selectionRect.right;
        const mouseY = window.event?.clientY || (selectionRect.top + selectionRect.height / 2);
        
        // 默认显示在鼠标右侧
        let left = mouseX + SPACING;
        let top = mouseY - (toolbarHeight / 2);

        // 处理右侧边界
        if (left + toolbarWidth + PADDING > window.innerWidth) {
            // 右侧空间不足，改为显示在鼠标左侧
            left = mouseX - toolbarWidth - SPACING;
            
            // 如果左侧也不够，则水平居中显示
            if (left < PADDING) {
                left = (window.innerWidth - toolbarWidth) / 2;
            }
        }

        // 处理垂直边界
        if (top < PADDING) {
            top = PADDING;
        } else if (top + toolbarHeight + PADDING > window.innerHeight) {
            top = window.innerHeight - toolbarHeight - PADDING;
        }

        return { left, top };
    }
}; 